﻿
using System;

using Bootstrap5.Abstractions;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;

namespace Bootstrap5;

[CssClass("toast")]
[HtmlRole("alert")]
public class Toast:BootstrapChildContentWithChildCascadingComponentBase<Toast,ToastContainer>
{


    /// <summary>
    /// Gets or sets the identifier.
    /// </summary>
    /// <value>
    /// The identifier.
    /// </value>
    [Parameter] public Guid Id { get; set; }
    [Parameter] public RenderFragment HeadContent { get; set; }
    [Parameter] public bool Closable { get; set; }
	[Parameter] [CssClass("bg-")]public Color? Color { get; set; }

	protected override void CreateCssClass(ICssClassBuilder builder)
	{
		if (Color.HasValue)
		{
            builder
                .Add(Color.Value.GetColorTextClass());
		}
	}

	protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, GetHtmlTagName());
        BuildCommonAttributes(builder);
        builder.AddContent(10, content =>
        {
            BuildHeadContent(content);
            content.CreateElement(20, "div", ChildContent, new { @class = "toast-body" });
        });
        builder.CloseElement();
    }

    void BuildHeadContent(RenderTreeBuilder builder)
    {
        if (HeadContent is null)
        {
            return;
        }
        builder.CreateElement(0, "div", content =>
        {
            content.AddContent(0, HeadContent);
            if (Closable)
            {
                content.OpenComponent<CloseButton>(10);
                content.AddAttribute(11, nameof(CloseButton.Trigger), Trigger.Dismiss(DismissName.Toast));
                content.CloseComponent();
            }
        }, new { @class = "toast-header" });
    }
}
